﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using AMM.Business;
using AMM.Business.Store;
using AMM.CustomWinControls;
using AMM.Model;
using AMM.Model.Store;
using AMM.UserControls;

namespace AMM.Forms.Store {
    public partial class FrmOutsourceQuotation : FrmOutsourceQuotationBase {

        private int _pageIndex;
        private OutsourceQuotationSearchModel _searchModel;
        private OutsourceQuotationProcessor _outsourceQuotationProcessor;
        private List<OutsourceQuotationViewModel> _outsourceQuotationList;

        #region Override

        protected override string FunctionCode {
            get { return "AMM1404"; }
        }

        protected override BaseSearchControl SearchControl {
            get { return new OutsourceQuotationSearchControl(); }
        }

        protected override void Add() {
            base.Add();
            this.ResetForms();
            this.ShowEditForm("新增", new OutsourceQuotation() { IsMultiple = false, ClassStatus = ClassStatus.Added });
        }

        protected override void Amend() {
            List<OutsourceQuotationViewModel> selectedList = this.dgvOutsourceQuotationList.GetSelectedRows<OutsourceQuotationViewModel>();
            if (selectedList.Count > 1) { this.OnlySelectSingleRecord(); return; }
            if (selectedList.Count == 0) { this.ShouldSelectSingleRecord(); return; }
            OutsourceQuotation outsourceQuotation = this._outsourceQuotationProcessor.FetchOutsourceQuotationById(selectedList[0].Key);
            outsourceQuotation.ClassStatus = ClassStatus.Updated;
            if ((AuditStatus)outsourceQuotation.Status == AuditStatus.Audited) {
                this.ShowInfo("审核的订单不能被修改！");
                return;
            }
            this.ShowEditForm("修改", outsourceQuotation);
            base.Amend();
        }

        protected override void Save() {
            base.Save();
        }

        protected override void Delete() {
            base.Delete();
            List<OutsourceQuotationViewModel> selectedList = this.dgvOutsourceQuotationList.GetSelectedRows<OutsourceQuotationViewModel>();
            if (selectedList.Count == 0) { this.ShouldSelectSingleRecord(); return; }
            this.CreateNewThread(() => {
                this.PreventCancel = true;
                this._outsourceQuotationProcessor.Delete(selectedList.Select(x => x.Key).ToArray());
                this.MDIParent.SetStatusInformation("删除成功");
            }, () => {
                this.ResetForms();
                this.PreventCancel = false;
            });
        }

        protected override void Audit() {
            List<OutsourceQuotationViewModel> selectedList = this.dgvOutsourceQuotationList.GetSelectedRows<OutsourceQuotationViewModel>();
            if (selectedList.Count > 1) { this.OnlySelectSingleRecord(); return; }
            if (selectedList.Count == 0) { this.ShouldSelectSingleRecord(); return; }
            this.CreateNewThread(() => {
                this.PreventCancel = true;
                string resultMsg = string.Empty;
                OutsourceQuotation outsourceOutsourceQuotation = this._outsourceQuotationProcessor.FetchOutsourceQuotationById(selectedList[0].Key);
                if (outsourceOutsourceQuotation.Status == (int)AuditStatus.Audited) {
                    if (this.ShowQuestion("订单已经通过审核，确定将取消审核状态！") == DialogResult.No) { return; }
                    outsourceOutsourceQuotation.Status = (int)AuditStatus.Waiting;
                    resultMsg = "取消审核成功";
                }
                else {
                    outsourceOutsourceQuotation.Status = (int)AuditStatus.Audited;
                    resultMsg = "审核成功";
                }
                outsourceOutsourceQuotation.AuditedBy = LoginUserName;
                this._outsourceQuotationProcessor.Audit(outsourceOutsourceQuotation);
                this.MDIParent.SetStatusInformation(resultMsg);
            }, () => {
                this.ResetForms();
                this.PreventCancel = false;
                base.Audit();
            });
        }

        protected override void Search(object sender, FrmSearch.SearchArgs e) {
            this.CreateNewThread(() => {
                if (e.IsRevert) {
                    this._searchModel = new OutsourceQuotationSearchModel();
                }
                else {
                    this._searchModel = (OutsourceQuotationSearchModel)e.SearchModel;
                }
            }, () => {
                this.BindOutsourceQuotationList();
            });
        }

        #endregion

        public FrmOutsourceQuotation() {
            InitializeComponent();

            this.dgvOutsourceQuotationList.DataGridViewModelType = typeof(OutsourceQuotationViewModel);
        }

        #region Private methods

        private bool frmEdit_Edit(object sender, FrmEdit.EditArgs e) {
            bool resultFlag = false;
            this.PreventCancel = true;
            OutsourceQuotation outsourceQuotation = (OutsourceQuotation)e.EditModel;
            this.CreateNewThread(() => {
                if (outsourceQuotation.ClassStatus == ClassStatus.Added) {
                    outsourceQuotation.CreatedBy = LoginUserName;
                    this._outsourceQuotationProcessor.Insert(outsourceQuotation);
                }
                else {
                    outsourceQuotation.ModifiedBy = LoginUserName;
                    this._outsourceQuotationProcessor.Update(outsourceQuotation);
                }
                resultFlag = true;
            }, () => {
                this.ResetForms();
                this.PreventCancel = false;
                if (resultFlag) {
                    this.MDIParent.SetStatusInformation("保存成功");
                    this.FormEdit.Close();
                }
            });
            this.Save();
            return true;
        }

        private void ResetForms() {
            this.BindOutsourceQuotationList();
        }

        private void BindOutsourceQuotationList() {
            this.CreateNewThread(() => {
                this._outsourceQuotationList = this._outsourceQuotationProcessor.FetchOutsourceQuotationViewModelList(this._searchModel, this._pageIndex);
            }, () => {
                this.dgvOutsourceQuotationList.DataSource = new SortableBindingList<OutsourceQuotationViewModel>(this._outsourceQuotationList);
                this.pceList.Records = this._searchModel.Records;
                this.pceList.DataBind();
            });
        }

        private void ShowEditForm(string title, OutsourceQuotation outsourceQuotation) {
            OutsourceQuotationEditControl editControl = new OutsourceQuotationEditControl();
            this.FormEdit = new FrmEdit();
            editControl.BaseEditModel = outsourceQuotation;
            editControl.ValidatorForEdit = this.FormEdit.validatorForEdit;
            this.FormEdit.Controls.Add(editControl);
            this.FormEdit.Edit += new FrmEdit.EditEventHandler(frmEdit_Edit);
            this.FormEdit.Text = string.Format("{0}{1}", title, this.Text);
            FrmEntry.LoadAndRunDialog(this.FormEdit);
        }

        #endregion

        private void FrmOutsourceQuotation_Load(object sender, EventArgs e) {
            this.CreateNewThread(() => {
                this.PreventCancel = true;
                this._outsourceQuotationProcessor = new OutsourceQuotationProcessor(this.pceList.PageSize);
                this._searchModel = new OutsourceQuotationSearchModel();
                this._pageIndex = 1;
            }, () => {
                this.WindowState = FormWindowState.Maximized;
                this.BindOutsourceQuotationList();
                this.PreventCancel = false;
            });
        }

        private void pceList_Paging(object sender, PaginationArgs e) {
            this._pageIndex = e.PageIndex;
            this.BindOutsourceQuotationList();
        }

    }
}
